package com.cangshi.permission.holder;

import com.cangshi.permission.entity.ErrorJSONObject;
import com.cangshi.permission.entity.Subject;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Created by Eoly on 2017/4/5.
 */
public class CurrentHolder {


    private final static ThreadLocal<Subject> CURRENT = new ThreadLocal<Subject>();

    public CurrentHolder() {
    }

    public static Subject getSubject(){
        return CURRENT.get();
    }

    public static void setSubject(Subject subject){
        CURRENT.set(subject);
    }

    public static void removeSubject(){
        CURRENT.remove();
    }

    public static void writeErrorUTF8(HttpServletResponse response, ErrorJSONObject errorJSONObject) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            out.write(errorJSONObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                out.flush();
                out.close();
            }
        }
    }
}
